package com.zds.support.util

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText


/**
 *
 * @Description: 软键盘相关辅助类
 * @author zc
 * @version V1.0
 */
object KeyBoardUtils {
    /**
     * 打开软键盘
     *
     * @param mEditText 输入框
     * @param mContext  下文
     */
    @JvmStatic
    fun openKeybord(context: Context, editText: EditText) {
        try {
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE)
            if (imm is InputMethodManager) {
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                        InputMethodManager.HIDE_IMPLICIT_ONLY)
            }
        } catch (e: Exception) {

        }


    }

    /**
     * 关闭软键盘
     *
     * @param mEditText 输入框
     * @param mContext 上下文
     */
    @JvmStatic
    fun closeKeybord(context: Context, editText: EditText) {
        try {
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE)
            if (imm is InputMethodManager) {
                imm.hideSoftInputFromWindow(editText.windowToken, 0)
            }
        } catch (e: Exception) {

        }
    }

    @JvmStatic
    fun closeKeybord(editText: EditText) {
        try {
            val context: Context = editText.context
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE)
            if (imm is InputMethodManager) {
                imm.hideSoftInputFromWindow(editText.windowToken, 0)
            }
        } catch (e: Exception) {

        }
    }


    @JvmStatic
    fun closeKeybord(context: Activity) {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE)
        val view: View = context.getWindow().peekDecorView();
        if (imm is InputMethodManager) {
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

}